#include "common.h"

static uint8_t mode = 0xee;
static uint8_t scence = DMOTION_MODE;
static uint8_t indoor_mode_restart = 0;

void app_init()
{
}

extern uint32_t GetUserData(uint8_t mode);


void set_currapp_mode(uint8_t value)
{
    scence = value;
}

uint8_t get_currapp_mode()
{
    return scence;
}

int app_callback(int value)
{
    if (value & EVENT_APP_START)
    {
        set_timer_event(APP_TASK, EVENT_APP_TIMER, 1000);
        value &= ~(EVENT_APP_START);
    }
      
    if (value & EVENT_APP_TIMER)
    {
        uint32_t delay = 0;
        ADXL345_Read();
        if (scence == STEP_MODE)
        {
            //mode one
            delay = GetUserData(0x1);
        }
        else if (scence == SLEEP_MODE)
        {
            //delay = GetUserData(0xee);
        }
        else if (scence == DMOTION_MODE)
        {
            /*  following code is solve the first time to use buffer will happen error, 
                                 so we run it with 10 times and do nothing  */
            if (indoor_mode_restart ++ > 5)
            {
                indoor_mode_restart = 6;
                delay = motion_know(mode, 0);
            }
            else
                delay = motion_know(mode, 1);
        }
        else if (scence == WAIT_MODE)
        {
            Doorkeeper();
            delay = 60;
        }
        
        if (delay < 100)
        {
            //high_mode();
             //printf("high\r\n");
        }
        else
        {
            //printf("low\r\n");
            //low_mode();
        }
        set_timer_event(APP_TASK, EVENT_APP_TIMER, delay);
    }
  
    return value;
}